<template>
   <div class="app-container">


    <el-form inline ref="searchTeacherForm" :model="searchTeacherForm" label-width="80px">
      <el-form-item label="讲师名称">
        <el-input v-model="searchTeacherForm.name"></el-input>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="searchTeacherForm.level" placeholder="请选择头衔">
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
        </el-select>
      </el-form-item>

      <!-- 添加讲师的时间范围 -->
      <el-form-item label="添加时间">
        <div class="block">
          
          <el-date-picker
            v-model="searchTeacherForm.listDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </div>
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
        <el-button @click="reset">置空</el-button>
      </el-form-item>
    </el-form>
    
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index +1}}
        </template>
      </el-table-column>
      <el-table-column label="姓名">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="头衔"   align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.level ==1 ? '高级讲师':'首席讲师' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资历"   align="center">
        <template slot-scope="scope">
          {{ scope.row.career }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="排序"   align="center">
        <template slot-scope="scope">
          <el-tag >{{ scope.row.sort }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center"   label="添加时间" >
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.eduCreate }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center"  label="修改时间"  >
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.eduModified }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"  label="操作"  >
        <template slot-scope="scope">
          <el-button type="primary" @click="handleClick(scope.row)" size="mini">修改</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)" size="mini">删除</el-button>
        </template>
        
      </el-table-column>
    
    </el-table>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="page.current"
        :page-size="page.size"
        layout="total, prev, pager, next"
        :total="page.total">
      </el-pagination>
    </div>

  </div>
</template>

<script>


import { getList ,deleteById} from '@/api/teacher'
export default {

  data() {
    return {
      list: null,
      listLoading: true,
      page: {
          current: 1,
          total: 0,
          size: 2
        },
      searchTeacherForm:{
        name:'',
        level:'',
        listDate:[]
      }

    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleClick(row) {
        this.$router.push({ path: '/teacher/save', query: { id: row.id } })
    },

    handleDelete(row){

      this.$confirm('此操作将删除该讲师, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          deleteById(row.id).then(response => {
            this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getPage()
        })
                    
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
        
    handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.page.current=val
        this.getPage()

    },
    reset(){
      this.searchTeacherForm={}
      this.getPage()
    },
    onSubmit() {
      console.log('submit!');
      this.getPage()

    },
    getPage(){
      getList(this.page.current,this.searchTeacherForm).then(response => {
        this.list = response.data.records
        this.page = response.data
        this.listLoading = false
      })
    },
    fetchData() {
      this.listLoading = true
      this.getPage()
    }
  }
}

</script>

<style>

</style>
